<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    

</head>

<body>
    <div id="root"></div>
</body>

<script src="https://unpkg.com/react@18.2.0/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom@18.2.0/umd/react-dom.development.js"></script>
<script src="https://cdn.staticfile.org/babel-standalone/6.26.0/babel.min.js"></script>
<script type="text/babel">
    let Context = React.createContext();
    class App extends React.Component {
        state = {
            date: [
                {
                    name: '张三',
                    age: '111'
                }
            ]
        }
        render() {
            const { date } = this.state
            return (
                <div className='box'>
                    <Context.Provider value={{
                        date,
                        gitDelete: (index) => {
                            this.state.date.splice(index, 1)
                            this.setState({
                                data: this.state.data
                            })
                        },
                        gitPush: (name, age) => {
                            this.state.date.push(
                                {
                                    name: name,
                                    age: age,
                                }
                            )
                            this.setState({
                                date: this.state.date
                            })
                        }
                    }}>
                        <List />
                        <Cart />
                    </Context.Provider>
                </div>
            )
        }
    }
    class List extends React.Component {
        state = {
            name: '',
            age: '',
        }
        render() {
            const { name, age } = this.state
            const { gitPush } = this.props
            console.log(age);
            return (
                <div className='list'>
                    <div> 评论人：</div>
                    <input type="text" onChange={(e) => this.setState({ name: e.target.value })} value={name} />
                    <div>评论内容：</div>
                    <textarea cols="80" rows="10" value={age}
                        onChange={(e) => this.setState({ age: e.target.value })}
                    ></textarea><br />
                    <Context.Consumer>
                        {
                            (value) => {
                                return <button onClick={() => value.gitPush(name, age)}>发表</button>
                            }
                        }
                    </Context.Consumer>

                </div >
            )
        }
    }
    class Cart extends React.Component {
        render() {
            return (
                <div className='cart'>
                    <div>留言板：</div>
                    <Context.Consumer>
                        {
                            (value) => {
                                return (value.date.map((item, index) =>
                                    <div key={index} className='cart-ps'>
                                        <p>作品：{item.name}</p>
                                        <p>内容：{item.age}</p>
                                        <div className='button'
                                            onClick={() => { value.gitDelete(index) }}><button>删除</button></div>
                                    </div>
                                ))

                            }
                        }
                    </Context.Consumer>
                </div>
            )
        }
    }
    let root = ReactDOM.createRoot(document.getElementById("root"))
    root.render(<App />)
</script>

</html>